package Classes
{
	import Classes.Actions.IAction;
	import Classes.Components.IComponent;
	
	import flash.utils.Dictionary;
	import flash.utils.getTimer;

	public class ComponentManager
	{
		private var m_componentsByType:Object = new Object;
		private var m_lastTick:int = 0;

		public function ComponentManager()
		{
		}
		
		public function addComponent(component:IComponent):void{
			var components:Vector.<IComponent> = m_componentsByType[component.getType()];
			if(!components){
				components = m_componentsByType[component.getType()] = new Vector.<IComponent>;
			}
			
			components.push(component);
		}
		
		public function removeComponent(component:IComponent):void{
			var components:Vector.<IComponent> = m_componentsByType[component.getType()];
			components.splice(components.indexOf(component),1);
		}

		
		public function update():void{
			var vector:Vector.<IComponent>;
			var i:int;
			var length:int;
			var curTick:int = getTimer();
			var delta:int = curTick - m_lastTick;
			
			for each(vector in m_componentsByType){
				length = vector.length;
				for(i=0;i<length;i++){
					vector[i].update(delta);
					if(vector[i].canDestroy()){
						vector[i].destroy();
						length--;
					}
				}
			}
			
			m_lastTick = curTick;
		}
	}
}